﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class DispatchRule : BasePage
    {
        protected Button btnAddDetail;
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnDeleteDetail;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkInterleaveTasks;
        protected CheckBox chkIsActive;
        protected CheckBox chkSysCreated;
        protected HtmlForm F2211;
        protected GridView gvDispatchRuleDetail;
        protected HtmlInputHidden hdnDispatchRuleID;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnSeqNo;
        protected HtmlHead Head1;
        protected Label lblDescr;
        protected Label lblDispatchRule;
        protected Label lblMsg;
        protected Label lblTaskRuleID;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected TextBox txtDescr;
        protected TextBox txtTaskRuleID;
        protected UpdatePanel up1;

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtTaskRuleID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2450_002");
            }
            else
            {
                if (string.IsNullOrEmpty(this.hdnDispatchRuleID.Value))
                {
                    this.InsertTaskRuleInfo();
                }
                base.Response.Redirect(string.Format("DispatchRuleDetail.aspx?from={0}&ID={1}&SeqNO={2}", base.UrlEncode(this.hdnFrom.Value), base.UrlEncode(this.txtTaskRuleID.Text.Trim()), base.UrlEncode(this.hdnSeqNo.Value)), true);
            }
        }

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtTaskRuleID.Text = "";
            this.txtTaskRuleID.Enabled = true;
            this.chkSysCreated.Checked = false;
            this.hdnDispatchRuleID.Value = "";
            this.hdnFlag.Value = "0";
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.chkSysCreated.Checked)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C011");
            }
            else if (!string.IsNullOrEmpty(this.txtTaskRuleID.Text.Trim()))
            {
                try
                {
                    List<string> taskRuleID = new List<string> {
                        this.txtTaskRuleID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(taskRuleID, "TASK_RULE") + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
                }
            }
        }

        protected void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtTaskRuleID.Text.Trim()) && (this.hdnFlag.Value == "1"))
            {
                List<string> taskRuleID = base.GetSelectedIDsInGridView(this.gvDispatchRuleDetail, "TaskRuleID");
                List<string> lineID = base.GetSelectedIDsInGridView(this.gvDispatchRuleDetail, "LineID");
                try
                {
                    this.lblMsg.Text = BaseInfoDetailServiceClientProxy.GetInstance().DeleteDetail(taskRuleID, lineID, new List<string>(), "TASK_RULE_DETAIL") + base.GetGlobalMsgResource("C006");
                    this.SetTaskRule2Form();
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C012") + ex.Message;
                }
            }
            else
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2450_004");
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("DispatchRuleQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("DispatchRuleList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtTaskRuleID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2450_002");
                this.txtTaskRuleID.Focus();
            }
            else if (string.IsNullOrEmpty(this.txtDescr.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2450_003");
                this.txtDescr.Focus();
            }
            else if (this.hdnFlag.Value == "1")
            {
                this.UpdateTaskRuleInfo();
            }
            else
            {
                this.InsertTaskRuleInfo();
            }
        }

        protected void gvDispatchRuleDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F2211", this.gvDispatchRuleDetail);
        }

        protected void gvDispatchRuleDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as TaskRuleInfo).TaskRuleDetailInfo;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
        }

        protected void gvDispatchRuleDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvDispatchRuleDetail.PageIndex * this.gvDispatchRuleDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
            }
        }

        protected void gvDispatchRuleDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                TaskRuleInfo taskRuleInfo = base.DataEntityViewState as TaskRuleInfo;
                DataView dv = CADataConverter.ToDataTable(taskRuleInfo.TaskRuleDetailInfo).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvDispatchRuleDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        private void InsertTaskRuleInfo()
        {
            try
            {
                TaskRuleInfo taskRule = new TaskRuleInfo();
                if (base.DataEntityViewState != null)
                {
                    taskRule = base.DataEntityViewState as TaskRuleInfo;
                    if ((taskRule.TaskRuleDetailInfo != null) && (taskRule.TaskRuleDetailInfo.Count > 0))
                    {
                        for (int i = 0; i < taskRule.TaskRuleDetailInfo.Count; i++)
                        {
                            taskRule.TaskRuleDetailInfo[i].TaskRuleID = this.txtTaskRuleID.Text.Trim();
                        }
                    }
                }
                taskRule.TaskRuleID = this.txtTaskRuleID.Text.Trim();
                taskRule.Descr = this.txtDescr.Text.Trim();
                taskRule.SystemCreated = this.chkSysCreated.Checked ? "Y" : "N";
                taskRule.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                taskRule.InterleaveTasks = this.chkInterleaveTasks.Checked ? "Y" : "N";
                taskRule.CreatedBy = base.UserInfo.UserID;
                this.hdnDispatchRuleID.Value = BaseInfoServiceClientProxy.GetInstance().Insert(taskRule, "TASK_RULE");
                this.lblMsg.Text = this.hdnDispatchRuleID.Value + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = taskRule;
                this.hdnFlag.Value = "1";
                this.txtTaskRuleID.Enabled = false;
                if ((taskRule.TaskRuleDetailInfo != null) && (taskRule.TaskRuleDetailInfo.Count > 0))
                {
                    this.hdnSeqNo.Value = (taskRule.TaskRuleDetailInfo[taskRule.TaskRuleDetailInfo.Count - 1].LineID + 10).ToString();
                }
                else
                {
                    this.hdnSeqNo.Value = "10";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnDispatchRuleID.Value = base.Request.QueryString["ID"].ToString();
                this.SetTaskRule2Form();
                this.hdnFlag.Value = "1";
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetTaskRule2Form()
        {
            try
            {
                string dispatchRuleID = this.hdnDispatchRuleID.Value;
                TaskRuleInfo taskRule = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(dispatchRuleID, "TASK_RULE") as TaskRuleInfo;
                base.DataEntityViewState = taskRule;
                if (taskRule != null)
                {
                    this.txtTaskRuleID.Text = taskRule.TaskRuleID;
                    this.txtDescr.Text = taskRule.Descr;
                    this.chkInterleaveTasks.Checked = taskRule.InterleaveTasks == "Y";
                    this.chkIsActive.Checked = taskRule.IsActive == "Y";
                    this.chkSysCreated.Checked = taskRule.SystemCreated == "Y";
                    if ((taskRule.TaskRuleDetailInfo != null) && (taskRule.TaskRuleDetailInfo.Count > 0))
                    {
                        this.hdnSeqNo.Value = (taskRule.TaskRuleDetailInfo[taskRule.TaskRuleDetailInfo.Count - 1].LineID + 10).ToString();
                    }
                    else
                    {
                        this.hdnSeqNo.Value = "10";
                    }
                    base.BindData2GridView(this.gvDispatchRuleDetail, taskRule.TaskRuleDetailInfo);
                    this.txtTaskRuleID.Enabled = false;
                    if (this.chkSysCreated.Checked)
                    {
                        this.chkIsActive.Enabled = false;
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateTaskRuleInfo()
        {
            try
            {
                TaskRuleInfo taskRule = base.DataEntityViewState as TaskRuleInfo;
                taskRule.Descr = this.txtDescr.Text.Trim();
                taskRule.SystemCreated = this.chkSysCreated.Checked ? "Y" : "N";
                taskRule.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                taskRule.InterleaveTasks = this.chkInterleaveTasks.Checked ? "Y" : "N";
                taskRule.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(taskRule, "TASK_RULE") + base.GetGlobalMsgResource("C004");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }
    }
}

